package com.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.utils.Result;
import com.entity.Node;
import com.service.CardService;
import com.service.NodeService;
import com.vo.MsgTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;


/**
 *
 */
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping("/node")
public class NodeController {

    @Autowired
    private NodeService nodeService;

    @Autowired
    private CardService cardService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;


    /**
     * addMsgTask
     */
    @RequestMapping("/addMsgTask")
    public Result addMsgTask(HttpServletRequest request){
        String time = request.getParameter("time");
        String str = request.getParameter("str");
        MsgTask msgTask=new MsgTask(str,Long.valueOf(time));
        threadPoolExecutor.execute(msgTask);
        return Result.success();
    }

    /**
     * 返回node
     */
    @RequestMapping("/getNode")
    public Result getNode(){
      return  nodeService.getNode();
    }

    /**
     * 获取任务节点
     * 下拉框使用
     */
    @RequestMapping("/getNodeList")
    public Result getNodeList(){
        return  nodeService.getNodeList();
    }

    /**
     * 添加node
     */
    @RequestMapping("/addNode")
    public Result addNode(@RequestBody Node node){
       return  nodeService.addNode(node);
    }

    /**
     * 删除节点
     */
    @RequestMapping("/delNode/{id}")
    public Result delNode(@PathVariable("id") Integer id){
        boolean insert = nodeService.deleteById(id);
        if(insert){
            return  Result.success("删除成功");
        }else{
            return  Result.error("删除失败");
        }
    }

    /**
     * 更新节点
     */
    @RequestMapping("/updateNode")
    public Result updateNode(@RequestBody Node node){
        boolean insert = nodeService.updateById(node);
        if(insert){
            return  Result.success("更新成功");
        }else{
            return  Result.error("更新失败");
        }
    }

    /**
     *  false true
     */
    public Result updateSign(){
        return Result.success("");
    }

    /**
     * 图表数据
     */
    public Result getData(){
        return  null;
    }

    /**
     * 添加组合任务，都完成，才算哇完成
     */
    @RequestMapping("/addGroupNode")
    public Result addGroupNode(@RequestBody Node node){
        List<Node> childNode = node.getChildNode();

        List<Integer> idList=new ArrayList<>();
        for (Node vo : childNode) {
            idList.add(vo.getId())  ;
        }
        Wrapper<Node> wr=new EntityWrapper<>();
        wr.in("id",idList);
        List<Node> nodes = nodeService.selectList(wr);

        return  null;
    }

    /**
     * 添加提示时间，和提示等级
     * label.setExecuteTime("[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]");
     */


}

